<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="detailData" :rules="rule" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="detailData.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="detailData.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="detailData.mobile"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <selectTree v-model="detailData.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="detailData.formOfEmployment" size="mini" class="inputW">
                  <el-option :value="1">正式</el-option>
                  <el-option :value="2">非正式</el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="detailData.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="detailData.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像" prop="staffPhoto">
                <!-- 放置上传图片 -->
                <imageUpload v-model="detailData.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveUpdate">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>

import selectTree from './components/selectTree.vue'
import { postEmployee, getEmployeeDetail, putEmployeeDeatil } from '@/api/employee'
import imageUpload from './components/imageUpload.vue'
export default {
  name: 'EmployeeDetail',
  components: { selectTree, imageUpload },
  data() {
    return {
      detailData: {
        username: '',
        formOfEmployment: null,
        workNumber: '',
        departmentId: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: '',
        mobile: ''
      },
      rule: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '字符只能1-4个', trigger: 'blur' }
        ],
        departmentId: [{ required: true, message: '部门不能为空', trigger: 'blur' }],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { max: 11, message: '手机号只能11位', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '手机号格式要13,14,15,16,17,18,19开头', trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '转正时间不能为空', trigger: 'blur' },
          { trigger: 'blur',
            validator: (value, rule, callback) => {
              if (this.detailData.timeOfEntry) {
                if (new Date(this.detailData.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                }
                callback()
              }
            }
          }
        ]
      }

    }
  },
  created() {
    this.$route.query.id && this.getEmployeeDetail()
  },
  methods: {
    saveUpdate() {
      this.$refs.userForm.validate(async isOk => {
        if (isOk) {
          if (this.$route.query.id) {
            await putEmployeeDeatil({ ...this.detailData, id: this.$route.query.id })
            this.$message.success('修改员工信息成功')
          } else {
            await postEmployee(this.detailData)
            this.$message.success('添加员工成功')
          }
          this.$router.push('/employee')
        }
      })
    },
    async  getEmployeeDetail() {
      this.detailData = await getEmployeeDetail(this.$route.query.id)
    }

  }
}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>
